package com.example.administrator.daydayappmarket.recommend.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.administrator.daydayappmarket.Contacts;
import com.example.administrator.daydayappmarket.R;
import com.example.administrator.daydayappmarket.recommend.moduls.GsonSpecial;

import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * Created by Administrator on 2016/12/28.
 */

public class RecommendTopSpecialAdapter extends BaseAdapter {
    private List<GsonSpecial> data;
    private Context context;

    public RecommendTopSpecialAdapter(List<GsonSpecial> data, Context context) {
        this.data = data;
        this.context = context;
    }

    @Override
    public int getCount() {
        return data != null ? data.size() : 0;
    }

    @Override
    public GsonSpecial getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder viewHolder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_recommend_top_image_special_second_listview, parent, false);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        }else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        GsonSpecial item = getItem(position);
        GsonSpecial.DataBean data = item.getData();
        if(data.getBanner().startsWith("http")){
            Glide.with(context).load(data.getBanner()).into(viewHolder.image);
        }else {
            Glide.with(context).load(Contacts.URL_BASEIMAGE+data.getBanner()).into(viewHolder.image);
        }
        viewHolder.text.setText(data.getTitle());

        return convertView;
    }

    static class ViewHolder {
        @InjectView(R.id.item_recommend_top_image_special_second_listView_image)
        ImageView image;
        @InjectView(R.id.item_recommend_top_image_special_second_listView_text)
        TextView text;

        ViewHolder(View view) {
            ButterKnife.inject(this, view);
        }
    }
}
